//
//  UIViewEmpty.swift
//  CommonTools
//
//  Created by xianquan on 2023/12/26.
//

import Foundation
import TYUIKit

public protocol EmptyContainer{
    
    var container: UIView {get}
    
    func emptyView(message: String, image: UIImage, imageSize: CGSize, reloadTitle: String?, reloadAction: (() -> ())?) -> TYEmptyView
}

extension EmptyContainer {
    @discardableResult public func emptyView(message: String, image: UIImage, imageSize: CGSize = CGSize(width: 240, height: 185), reloadTitle: String? = nil, reloadAction: (() -> ())? = nil) -> TYEmptyView{
        let emptyView = container.ty_emptyView as? TYEmptyView ?? TYEmptyView(frame: container.bounds)
        emptyView.image = image
        emptyView.imageViewSize = TY_ScreenAdaptionSize(imageSize)
        emptyView.title = message
        emptyView.titleFont = .systemFont(ofSize: adapt(16), weight: .regular)
        emptyView.titleColor = .black3
        emptyView.titleOffset = CGPoint(x: 0, y: adapt(10))
        if let reloadTitle = reloadTitle {
            let btnWidth = reloadTitle.width(for: adapt(16) ) + adapt(73.0)
            let btn = UIButton(title: reloadTitle, titleColor: .white, imageInBundle: nil, font: adapt(16), in: nil, frame: CGRect(x: 0, y: 0, width: btnWidth, height: adapt(35)))
            btn.setBackgroundColor(.selectedColor, for: .normal)
            btn.cut(withRadius: btn.height / 2.0)
            if let reloadAction = reloadAction {
                btn.addBlock(for: .touchUpInside) { _ in
                    reloadAction()
                }
            }
            emptyView.actionButtonOffset = CGPoint(x: 0, y: adapt(20))
            emptyView.actionArr = [btn]
        }
        container.ty_emptyView = emptyView
        
        return emptyView
    }
}


extension UIView: EmptyContainer{
    public var container: UIView {
        self
    }
}

extension UIViewController: EmptyContainer{
    public var container: UIView {
        self.view
    }
}
